import 'package:flutter/services.dart';
import 'package:share_fit/app/ute/uteped_const.dart';
import 'package:share_fit/app/tools/log_utils.dart';
import 'package:uteped_sdk/uteped_sdk.dart';

mixin UTEPedMixin {

  final uteSdk = UTEPedSdk.uteSdk;

  Future<bool> bRemoteReq(String method, Map<String, Object> params) async {
    /// 原生执行同步返回bool类型 `bRemoteReq`
    /// 需要原生执行的方法 [method]
    /// 参数 [params]
    bool result = false;
    try {
      result = await uteSdk.bFunc(_buildParams(method, params));
      logger.d("bRemoteReq method:$method params:$params result: $result");
    } on PlatformException {
      logger.d("bRemoteReq method:$method params:$params exception!");
    }
    return result;
  }

  Future<bool> bRemoteReqAsync(String method, Map<String, Object> params) {
    /// 原生执行异步返回bool类型 `bRemoteReq`
    /// 需要原生执行的方法 [method]
    /// 参数 [params]
    logger.d("bRemoteReqAsync method:$method params:$params");
    return Future(() => uteSdk.bFunc(_buildParams(method, params)));
  }

  Future<String> sRemoteReq(String method, Map<String, Object> params) async {
    /// 原生执行同步返回String类型 `sRemoteReq`
    /// 需要原生执行的方法 [method]
    /// 参数 [params]
    String result = "";
    try {
      result = await uteSdk.sFunc(_buildParams(method, params));
      logger.d("sRemoteReq method:$method params:$params result: $result");
    } on PlatformException {
      logger.d("bRemoteReq method:$method params:$params exception!");
    }
    return result;
  }

  Future<String> sRemoteReqAsync(String method, Map<String, Object> params) {
    /// 原生执行异步返回String类型 `sRemoteReq`
    /// 需要原生执行的方法 [method]
    /// 参数 [params]
    logger.d("sRemoteReqAsync method:$method params:$params");
    return Future(() => uteSdk.sFunc(_buildParams(method, params)));
  }

  Future<dynamic> dRemoteReq(
      String method, Map<String, Object> params) async {
    /// 原生执行同步返回Map类型 `sRemoteReq`
    /// 需要原生执行的方法 [method]
    /// 参数 [params]
    dynamic result = {};
    try {
      result = await uteSdk.dFunc(_buildParams(method, params));
    } on PlatformException {
      logger.d("bRemoteReq method:$method params:$params exception!");
    }
    return result;
  }

  Future<dynamic> dRemoteReqAsync(
      String method, Map<String, Object> params) {
    /// 原生执行异步返回Map类型 `sRemoteReq`
    /// 需要原生执行的方法 [method]
    /// 参数 [params]
    logger.d("dRemoteReqAsync method:$method params:$params");
    return Future(()=>uteSdk.dFunc(_buildParams(method, params)));
  }

  Future<void> vRemoteReq(String method, Map<String, Object> params) async {
    /// 原生执行同步void类型 `bRemoteReq`
    /// 需要原生执行的方法 [method]
    /// 参数 [params]
    try {
      await uteSdk.vFunc(_buildParams(method, params));
    } on PlatformException {
      logger.d("bRemoteReq method:$method params:$params exception!");
    }
  }

  Future<void> vRemoteReqAsync(String method, Map<String, Object> params) async {
    /// 原生执行异步void类型 `bRemoteReq`
    /// 需要原生执行的方法 [method]
    /// 参数 [params]
    logger.d("vRemoteReqAsync method:$method params:$params");
    Future(()=>uteSdk.vFunAsync(_buildParams(method, params)));
  }

  Map<String, Object> _buildParams(String method, Map<String, Object> params) {
    Map<String, Object> remoteParams = {UTEPedConst.keyMethod: method};
    if (!params.containsKey(UTEPedConst.keyPath)) {
      remoteParams[UTEPedConst.keyPath] = getRemotePath();
    }
    if (params.isNotEmpty) {
      remoteParams.addAll(params);
    }

    logger.d(
        "remote request path:$getRemotePath() method:$method params:$remoteParams");

    return remoteParams;
  }

  List<String> getSupportedMethods() {
    /// 支持方法列表，配合useMethodSupport使用，不依赖remotePath访问任何sdk函数
    return [];
  }

  bool useMethodSupport() {
    // 是否采用支持方法的形式
    return false;
  }

  /// 提供原生请求接口路径
  String getRemotePath();

  /// 原生端执行完成, 目前没有实现
  void onRemoteDone() {}

  /// 原生端执行报错
  void onRemoteError(error) {}
}
